<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link href="${ctx}/css/style.css" type="text/css" rel="stylesheet" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>左边</title>
        <link rel="stylesheet" href="${ctx}/css/jquery.treeview.css" />
        <script src="${ctx}/js/jquery-1.7.2.min.js" type="text/javascript"></script>
        <script src="${ctx}/js/jquery.cookie.js" type="text/javascript"></script>
        <script src="${ctx}/js/jquery.treeview.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(	function() {
                    var createLi=function(menu,parent){
                        var li=$("<li>");
                        var hasChild=menu.children.length>0;
                        if(hasChild){
                            li.append($("<span>").text(menu.title));
                            var ul = $("<ul>");
                            li.append(ul);
                            parent.append(li);
                            for(i in menu.children){
                                createLi(menu.children[i], ul);
                            }
                        }else{
                            li.append($("<span>").text(menu.title).attr("href","${ctx}" + menu.href));
                            parent.append(li);
                        }
                    };
                    var sort=function(items){
                         for (var i = 0; i < items.length; i++) {
                              for(var j = i+1; j < items.length; j++) {
                               if (items[i].sortBy > items[j].sortBy) {
                                t = items[i];
                                items[i] = items[j];
                                items[j] = t;
                               }
                              }
                              if(items[i].children.length>0){
                                  items[i].children=sort(items[i].children);
                              }
                         }
                         return items;
                    };
                    var buildTreeDOM=function(){
                        //链接加上参数 避免同一游览器 切换用户登录时候 菜单栏 被缓存
                        var no_cache = '?v=${PC_ACCOUNT.aid}' + '<s:date name="#session.PC_ACCOUNT.loginTime" format="yyyyMMddHHmmss"/>';
                        $.ajaxSetup({ async: false  });
                        $.getJSON("${ctx}/admin/menus.do" + no_cache,{},function(data){
                            data=sort(data);
                            $.each(data,function(i,n){
                                createLi(n,$("#navigation"));
                            });
                            $("#navigation").treeview({
                                animated : "fast",
                                collapsed : true,
                                unique : true,
                                toggle : function() { }
                            });
                            $("#navigation li span").each(function(i, n) {
                                if ($(n).attr("href") != undefined) {
                                    $(n).click(function() {
                                        var href = $(this).attr("href");
                                        if(href ==''||href == undefined||href=="${ctx}"){
                                            alert("此功能还在开发中..");
                                            return;
                                        }
                                        window.top.frames["mainFrame"].location.href = $(this).attr("href");
                                    });
                                }
                            });
                        });
                        $.ajaxSetup({ async: true  });
                    };

                    buildTreeDOM();

                    $('#navigation li:not(:has(ul))  span')
                        .mouseover(function() {
                                    this.style.background = "url(${ctx}/images/m_l_l_leftsidebar.gif)";
                                }).mouseout(function() {
                            this.style.background = "none";
                    });
                });
        </script>
    </head>

    <body>
        <div class="leftMain" style="width:214px; overflow:hidden;">

            <div class="left">
                <ul id="navigation">
                </ul>
            </div>
        </div>
    </body>
</html>

